package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.entity.Users;
import com.example.springboot.mapper.UsersMapper;
import com.example.springboot.util.ImageCode;
import com.example.springboot.util.R;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping()
@RestController
public class IndexController {

    @GetMapping("/index")
    public String index(){
        return "hello";
    }
    @Resource
    UsersMapper usersMapper;


    @PostMapping("/login")
    public R login(@RequestBody Map<String,Object> map){
        return R.ok(map);
    }

    @GetMapping("/info")
    public R info(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        return R.ok(new HashMap<String,Object>(){{
            put("name",name);
        }});
    }

    @GetMapping("/imageCode")
    public void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ImageCode imageCode = ImageCode.createImage();
        ImageIO.write(imageCode.getImage(),"JPEG",response.getOutputStream());
    }

    @GetMapping("/db/select")
    public List<Users> dbselect(HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper wrapper = new QueryWrapper();
        return usersMapper.selectList(wrapper);
    }

}
